package hr.fer.neumre.gesturerecognizer;

import hr.fer.neumre.gesturerecognizer.neural.Brain;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureOverlayView;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

/**
 * Aktivnost koja sluzi za prepoznavanje geste. Korisnik prvo nacrta gestu na ekranu a potom se pokaze tost sa nazivom
 * geste ukoliko ju je sustav uspio prepoznati.
 * @author Kresimir
 *
 */
public class RecognizeGestureActivity extends Activity {
	private static final float LENGTH_THRESHOLD = 120.0f;
    private Gesture mGesture;
    private Brain brain;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.recognize_gesture);
		
		GestureOverlayView overlay = (GestureOverlayView) findViewById(R.id.gestures_overlay_recognize);
        overlay.addOnGestureListener(new GesturesProcessor());
        
        brain = Brain.getBrain(); 
	}

	private void recognizeGesture(){
		String name = brain.recognize(mGesture);
        Toast.makeText(this, name, Toast.LENGTH_LONG).show();
	}
	
	private class GesturesProcessor implements GestureOverlayView.OnGestureListener {
        public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
            mGesture = null;
        }

        public void onGesture(GestureOverlayView overlay, MotionEvent event) {
        }

        public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
            mGesture = overlay.getGesture();
            if (mGesture.getLength() < LENGTH_THRESHOLD) {
                overlay.clear(false);
            }
            recognizeGesture();
        }

        public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
        }
    }
}
